﻿using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SouthernLand.Common.BOL.NhaDat
{
    [ParseChildren(true)]
    public class ddlCategories : DropDownList
    {
        public ddlCategories()
        {
            this.Items.Insert(0, new ListItem(ResourceManager.GetString("PleaseSelect"), ""));
            List<Categories> rootCategories = Categories.GetCategoriesByParentID((int)eCategory.RootOfCategories); // get root level ones

            if (rootCategories.Count > 0)
            {
                foreach (Categories c in rootCategories)
                {
                    string strTree = "";
                    this.CssClass = "dropdownlistParent";
                    ListItem l = new ListItem(strTree + ((Globals.LangVietNam()) ? c.Title : c.Title_en), c.CateID.ToString());
                    l.Attributes.Add("style", "font-weight:Bold;");
                    this.Items.Add(l);
                    populateCategoriesAdd(c.CateID);
                }
            }
            else
            {
                this.Items.Insert(0, new ListItem("Categories", "1"));
            }

        }

        private void populateCategoriesAdd(int ParentID)
        {
            string strTree = "|--";
            List<Categories> rootCategories = Categories.GetCategoriesByParentID(ParentID); // get root level ones
            foreach (Categories c in rootCategories)
            {
                ListItem l = new ListItem(strTree + ((Globals.LangVietNam()) ? c.Title : c.Title_en), c.CateID.ToString());
                this.Items.Add(l);
                populateCategoriesAdd2(c.CateID);
            }
        }

        private void populateCategoriesAdd2(int ParentID)
        {
            string strTree = "|-----";
            List<Categories> rootCategories = Categories.GetCategoriesByParentID(ParentID); // get root level ones
            foreach (Categories c in rootCategories)
            {
                ListItem l = new ListItem(strTree + ((Globals.LangVietNam()) ? c.Title : c.Title_en), c.CateID.ToString());
                this.Items.Add(l);
                populateCategoriesAdd2(c.CateID);
            }
        }

    }
}
